/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				 All right reserved. 		  </p>
 * 
 * <p>项目名称 ： 东莞交通工程质量监督综合业务系统 	  </p>
 * <p>创建者   :	ce 
 * 
 * <p>描   述  :   for CodeAction    </p>
 * 
 * <p>最后修改 : $: 2011-5-5-上午11:53:41 v 1.0.0	 ce   $ </p>
 * 
*/
package com.pengtu.web.app;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.entity.app.Attachment;
import com.pengtu.service.app.SystemManager;
import com.pengtu.utils.FileUtils;
import com.pengtu.utils.UploadFile;
import com.pengtu.utils.ZipHelper;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 码表管理管理Action.
 * 
 * 码表的CRUD 
 * 
 * @author ce
 */
@Namespace("/attachment")
public class AttachmentAction extends BaseActionSupport<Attachment> {
	private static final long serialVersionUID = -4052047494894591406L;
	private SystemManager systemManager;


	// -- 页面属性 --//
	private String id;
	private String partId; // 对应与某一部分
	private String tableId; // 对应表id
	private String tableName; // 对应某表
	private String tableField;	// 对应表中的某个字段
	private File upfile; // 文件
	private String upfileContentType; // 文件类型
	private String upfileFileName; // 文件原始名
	private Attachment entity;
//	private Page<Attachment> page = new Page<Attachment>(10);// 每页10条记录

	// -- ModelDriven 与 Preparable函数 --//
	public Attachment getModel() {
		return entity;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = systemManager.getAttachment(id);
		} else {
			entity = new Attachment();
		}
	}
	@Override
	public String list() throws Exception {
		List<Attachment> Attachments = systemManager.getAttachmentList(partId, tableName, tableId, tableField);
		Struts2Utils.renderJsontoExtTotalResults(Attachments);
		return NONE;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		systemManager.uploadFile(Struts2Utils.getAppRootPath(), entity);
		Struts2Utils.renderHtml(Constants.EXT_SUCCESS_JSON_STR);
		return NONE;
	}

	@Override
	public String delete() throws Exception {
		systemManager.deleteAttachment(Struts2Utils.getAppRootPath(),id);
		Struts2Utils.renderJson(Constants.EXT_SUCCESS_JSON_STR);
		return NONE;
	}
	
	/*用于js删除附件*/
	public void deletePnp() throws Exception {
		systemManager.deleteAttachment(Struts2Utils.getAppRootPath(),id);
	}

	
	public void downloadMany() throws Exception {
		List<Attachment> Attachments = systemManager.getAttachmentList(partId, tableName, tableId, tableField);
		Long currentDate = System.currentTimeMillis();
		String toFilename = System.getProperty("java.io.tmpdir")+currentDate + Constants.FILE_SEP;
		String toFile = toFilename+currentDate + Constants.FILE_SEP;
		(new File(toFile)).mkdirs();
		for(int i = 0 ; i < Attachments.size() ; i++){
			Attachment attachment = Attachments.get(i);
			String fromFile = Struts2Utils.getAppRootPath()+Constants.FILE_SEP + attachment.getFullPath();
			FileUtils.copy(fromFile, toFile + attachment.getUpfileFileName(), false);
		}
        String a  = currentDate+".zip";
		ZipHelper.getZipHelper().zip(toFile, toFilename+a);
		UploadFile.downloadFileByInputStream(Struts2Utils.getRequest(), Struts2Utils.getResponse(), new File(toFilename+a));
//		Struts2Utils.sendRedirectUrl("/servlet/annex?path="+toFilename+a);
	}
	public String getId() {
		return id;
	}

	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	public String getPartId() {
		return partId;
	}

	public void setPartId(String partId) {
		this.partId = partId;
	}

	public String getTableId() {
		return tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getTableField() {
		return tableField;
	}

	public void setTableField(String tableField) {
		this.tableField = tableField;
	}

	public File getUpfile() {
		return upfile;
	}

	public void setUpfile(File upfile) {
		this.upfile = upfile;
	}

	public String getUpfileContentType() {
		return upfileContentType;
	}

	public void setUpfileContentType(String upfileContentType) {
		this.upfileContentType = upfileContentType;
	}

	public String getUpfileFileName() {
		return upfileFileName;
	}

	public void setUpfileFileName(String upfileFileName) {
		this.upfileFileName = upfileFileName;
	}

	// -- 页面属性访问函数 --//

}